package com.googlecode.dgwt.client.dijit.view.render;

import java.util.List;

import com.google.gwt.dom.client.Element;
import com.googlecode.dgwt.client.dijit.Dijit;
import com.googlecode.dgwt.client.dijit.base.widget.WidgetEngine;
import com.googlecode.dgwt.client.dijit.view.render.base.CellRender;
import com.googlecode.dgwt.client.dijit.view.render.base.ViewRenderBase;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class ScrollRender extends ViewRenderBase implements CellRender {

    private final CellRender targetRender;    
    // TODO : sistema de renderização para criar rows e cells somente quando estiverem visíveis.
    private final int minimumRowsInCache;

    
    public ScrollRender(CellRender targetRender){
        this(targetRender, true);
    }

    public ScrollRender(CellRender targetRender, boolean rowEmulation){
        this(targetRender, rowEmulation ? 75 : -1);
    }
    
    public ScrollRender(CellRender targetRender, int minimumRowsInCache){
        super(targetRender.getView());
        this.targetRender = targetRender;
        this.minimumRowsInCache = minimumRowsInCache;
    }

    
    public CellRender getTargetRender() {
        return targetRender;
    }
    
    public boolean isRowEmulation(){
        return getMinimumRowsInCache() > -1;
    }
    
    public int getMinimumRowsInCache() {
        return minimumRowsInCache;
    }

    @Override
    protected void doBuildRendering() {
        getEngine().loadFromTemplate(Dijit.TEMPLATE_RESOURCES.indexedView());
        Element domElement = getWidget().getElement();
        Element scrollableElement = getEngine().getAttachedElement(WidgetEngine.SCROLLABLE_ELEMENT);
        
        getTargetRender().buildRendering();
        getEngine().setAttachedElement(WidgetEngine.FOCUSABLE_ELEMENT, scrollableElement);
        scrollableElement.appendChild(getWidget().getElement());
        
        getEngine().setDomElement(domElement);
        getEngine().setAttachedElement(WidgetEngine.SCROLLABLE_ELEMENT, scrollableElement);
    }

    @Override
    public void addCell(int cellIndex) {
        getTargetRender().addCell(cellIndex);
    }

    @Override
    public void removeCell(int cellIndex) {
        getTargetRender().removeCell(cellIndex);
    }

    @Override
    public void setCellCount(int cellCount) {
        getTargetRender().setCellCount(cellCount);
    }

    @Override
    public void updateCell(int rowIndex, int cellIndex) {
        getTargetRender().updateCell(rowIndex, cellIndex);
    }

    @Override
    public void updateCells(int cellIndex) {
        getTargetRender().updateCells(cellIndex);
    }

    @Override
    public void updateCells(Object metaInf, int cellIndex) {
        getTargetRender().updateCells(metaInf, cellIndex);
    }

    @Override
    public void updateCells(int cellIndex, int cellCount) {
        getTargetRender().updateCells(cellIndex, cellCount);
    }

    @Override
    public void updateCells(int rowIndex, int rowCount, int cellIndex, int cellCount) {
        getTargetRender().updateCells(rowIndex, rowCount, cellIndex, cellCount);
    }

    @Override
    public void addRow(int rowIndex) {
        getTargetRender().addRow(rowIndex);
    }

    @Override
    public void addRow(int rowIndex, Object metaInf) {
        getTargetRender().addRow(rowIndex, metaInf);
    }

    @Override
    public void addRows(int rowIndex, int rowCount) {
        getTargetRender().addRows(rowIndex, rowCount);
    }

    @Override
    public void removeRow(int rowIndex) {
        getTargetRender().removeRow(rowIndex);
    }

    @Override
    public void removeRows(int rowIndex, int rowCount) {
        getTargetRender().removeRows(rowIndex, rowCount);
    }

    @Override
    public void setRowCount(int rowCount) {
        getTargetRender().setRowCount(rowCount);
    }

    @Override
    public void updateRow(int rowIndex) {
        getTargetRender().updateRow(rowIndex);
    }

    @Override
    public void updateRow(int rowIndex, Object metaInf) {
        getTargetRender().updateRow(rowIndex, metaInf);
    }

    @Override
    public void updateRows(int rowIndex, int rowCount) {
        getTargetRender().updateRows(rowIndex, rowCount);
    }

    @Override
    public void updateRows(Object metaInf) {
        getTargetRender().updateRows(metaInf);
    }

    @Override
    public void updateRows() {
        getTargetRender().updateRows();
    }

    @Override
    public Element getRow(int rowIndex) {
        return getTargetRender().getRow(rowIndex);
    }

    @Override
    public int getRowCount() {
        return getTargetRender().getRowCount();
    }

    @Override
    public int getRowIndex(Element rowElement) {
        return getTargetRender().getRowIndex(rowElement);
    }

    @Override
    public List<Element> getRows() {
        return getTargetRender().getRows();
    }

    @Override
    public Element getCell(int rowIndex, int cellIndex) {
        return getTargetRender().getCell(rowIndex, cellIndex);
    }

    @Override
    public int getCellCount() {
        return getTargetRender().getCellCount();
    }

    @Override
    public int getCellIndex(Element cellElement) {
        return getTargetRender().getCellIndex(cellElement);
    }

    @Override
    public List<Element> getRowCells(int rowIndex) {
        return getTargetRender().getRowCells(rowIndex);
    }

}
